package com.manager.cms.controller;

import com.manager.cms.annotation.IgnoreAuth;
import com.manager.cms.annotation.IgnoreLogin;
import com.manager.cms.bo.ExpressMan;
import com.manager.cms.bo.SysAdmin;
import com.manager.cms.bo.UserInfo;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.service.ExpressManService;
import com.manager.cms.service.SysAdminService;
import com.manager.cms.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private SysAdminService sysadminservice;

    @Autowired
    private UserInfoService userinfoservice;

    @Autowired
    private ExpressManService expressmanservice;

    @IgnoreAuth
    @IgnoreLogin
    @PostMapping("/login")
    public CommonResponse login(String type, String username, String password) {

        switch (type) {
            case "sysAdmin":
                SysAdmin sysAdmin = new SysAdmin();
                sysAdmin.setUsername(username);
                List<SysAdmin> list = sysadminservice.getList(sysAdmin);
                if (list.size() > 0) {
                    if (password != null && password.equals(list.get(0).getPassword())) {
                        return CommonResponse.succeed(list.get(0));
                    } else {
                        return CommonResponse.fail("密码错误");
                    }
                }
                break;
            case "userInfo":
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(username);
                List<UserInfo> userInfos = userinfoservice.getList(userInfo);
                if (userInfos.size() > 0) {
                    if (password != null && password.equals(userInfos.get(0).getPassword())) {
                        return CommonResponse.succeed(userInfos.get(0));
                    } else {
                        return CommonResponse.fail("密码错误");
                    }
                }
                break;
            case "expressMan":
                ExpressMan expressMan = new ExpressMan();
                expressMan.setUsername(username);
                List<ExpressMan> expressManList = expressmanservice.getList(expressMan);
                if (expressManList.size() > 0) {
                    if (password != null && password.equals(expressManList.get(0).getPassword())) {
                        return CommonResponse.succeed(expressManList.get(0));
                    } else {
                        return CommonResponse.fail("密码错误");
                    }
                }
                break;
        }
        return CommonResponse.fail("用户名错误");
    }
}
